	//---------------------------------------------------------------------
	// Includes
	//---------------------------------------------------------------------			
	#include <assert.h>
	#include <Rendering/FrameBuffer.hpp>
	#include <Rendering/Renderer.hpp>
	
namespace Rendering {

	//---------------------------------------------------------------------
	// Texture manager methods	
	//---------------------------------------------------------------------
	FrameBuffer::FrameBuffer(int w, int h):
	width(w),height(h) {
		frameBufferID = Renderer::Instance().CreateFrameBuffer(width,height);
	}
	//---------------------------------------------------------------------
	FrameBuffer::~FrameBuffer() {
		Renderer::Instance().DeleteFrameBuffer(frameBufferID);
		frameBufferID = 0;
	}
	//---------------------------------------------------------------------
	void FrameBuffer::AddTarget(TargetType type, PixelFormat format) {
		TRenderingID componentID = Renderer::Instance().CreateTarget(frameBufferID, width, height, type, format);
		// Create texture
		TITexturePointer tex(new ITexture(SQUARE_TEXTURE, format,width,height,componentID));
		// Search target
		TTargetVector::iterator it;
		it = targets.begin();
		while(it != targets.end() && it->type != type)
			++it;
		// Replace target if it exists or create & add it
		if(it!= targets.end()) {
			it->target = tex;
		}
		else {
			Target target;
			target.target = tex;
			target.type = type;
			targets.push_back(target);
		}		
	}
	//---------------------------------------------------------------------
	TITexturePointer FrameBuffer::GetTarget(TargetType type) {				
		// Search target
		TTargetVector::iterator it;
		it = targets.begin();
		while(it != targets.end() && it->type != type) 
			++it;
		// Check result
		assert(it != targets.end());
		return it->target;			
	}
	//---------------------------------------------------------------------
	void FrameBuffer::Enable() {
		Renderer::Instance().EnableFrameBuffer(frameBufferID, width, height);
	}
	//---------------------------------------------------------------------
	void FrameBuffer::Disable() {
		Renderer::Instance().DisableFrameBuffer(frameBufferID);
	}
	//---------------------------------------------------------------------
	TFrameBufferPointer FrameBuffer::CreateFrameBuffer(int width, int height) {
		return TFrameBufferPointer(new FrameBuffer(width,height));
	}
	//---------------------------------------------------------------------
}
